package cn.laixueit.controller;

import cn.laixueit.pojo.item.Sku;
import cn.laixueit.pojo.item.Spu;
import cn.laixueit.pojo.item.SpuDetail;
import cn.laixueit.pojo.result.PageResult;
import cn.laixueit.pojo.vo.SpuVo;
import cn.laixueit.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

@Controller
public class GoodsController {

    @Autowired
    private GoodsService goodsService;


    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuVo>> page(@RequestParam(value = "key",required = false) String key,
                                                  @RequestParam(value = "saleable",required = false) Boolean saleable,
                                                  @RequestParam(value = "page",defaultValue = "1") Integer page,
                                                  @RequestParam(value = "rows",defaultValue = "5") Integer rows){
        PageResult<SpuVo> spuVos = goodsService.page(key,saleable,page,rows);

        return ResponseEntity.ok(spuVos);
    }

    @PostMapping("/goods")
    public ResponseEntity save(@RequestBody SpuVo spuVo){
        goodsService.save(spuVo);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据spuid查询对应详情
     */
    @GetMapping("/spu/detail/{id}")
    public ResponseEntity<SpuDetail> detail(@PathVariable("id")Long id){
        SpuDetail spuDetail = goodsService.spuDetail(id);
        return ResponseEntity.ok(spuDetail);
    }

    /**
     *
     * @param id spuid
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<Collection<Sku>> list(@RequestParam("id") Long id){
        List<Sku> skuList =  goodsService.skuList(id);
        return  ResponseEntity.ok(skuList);
    }

    @PutMapping("/goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuVo spuVo){
        goodsService.updateGoods(spuVo);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    /**
     * 根据spuid查询spu
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<Spu> findSpuById(@PathVariable("id")Long id){
        Spu spu = goodsService.findSpuId(id);
        return ResponseEntity.ok(spu);
    }

    /**
     * 根据skuid查询sku
     */
    @GetMapping("/sku")
    public ResponseEntity<Sku> getSkuById(@RequestParam("skuid")Long skuid){
        return  ResponseEntity.ok(goodsService.findSkuById(skuid));
    }

}
